<?php

namespace Application\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;

class ActivitesSejourTable
{
    protected $table ='ActivitesSejour';
    protected $tableName ='ActivitesSejour';
    
    protected $tableGateway;
    
    public function __construct(TableGateway $tableGateway)
    {
    	$this->tableGateway = $tableGateway;
    }
    
    public function fetchAll()
    {
    	$resultSet = $this->tableGateway->select(function(Select $select){
	        $select->order('libelleAS ASC');
	    });
    	
    	return $resultSet;
    }
    
    public function fetchAllArray()
    {
        $resultSet = $this->fetchAll();
        $entities = array();
        
        foreach ($resultSet as $row) {
        	$obj = new ActivitesSejour();
        	$obj->exchangeArray( (array) $row);
        	$entities[] = $obj;
        }
        
        return $entities;
    }

    /* retourne toutes les régions utilisés dans des séjours */
    public function fetchAllUsed()
    {
        $sql = new Sql($this->tableGateway->getAdapter());
        $select = $sql->select();

        $select->from($this->tableGateway->table)
                ->quantifier('DISTINCT')
                ->join('Sejour', 'Sejour.activiteId = ActivitesSejour.idAS', array(), 'inner')
                ->order('libelleAS ASC');
        
        $statement = $sql->prepareStatementForSqlObject($select);
        $resultSet = $statement->execute();

        return $this->toArray ($resultSet);
    }

    public function toArray ($resultSet)
    {
        $entities = array();
        
        foreach ($resultSet as $row) {
            $obj = new ActivitesSejour();
            $obj->exchangeArray( (array) $row);
            $entities[] = $obj;
        }
        
        return $entities;
    }

    public function initFiltresActivites ( $values )
    {
        $sql = new Sql($this->tableGateway->getAdapter());
        $select = $sql->select();
        
        $select->from($this->tableGateway->table)
                ->where->in("ActivitesSejour.idAS", $values);

        $statement = $sql->prepareStatementForSqlObject($select);
        $resultSet = $statement->execute();
        
        return $this->toArray ($resultSet);
    }

    public function getActiviteByLibelle( $libelle )
    {
        $rowset = $this->tableGateway->select(array('libelleAS' => ucfirst( $libelle ) ));
        $row = $rowset->current();
        
        return $row;
    }
}